//页面加载
$(document).ready(function(){
    loadGrid();
});

function loadGrid() {
    var url;
    var clientH = document.documentElement.clientHeight;
    var offsetH =clientH - 61;
    var pageSize = 10;
    rowHeight = 27;
    $('#licenseListTab').datagrid({
        width:'100%',
        height:offsetH,
        idField: 'id',
        nowrap: false,
        striped: true,
        collapsible:false,//是否可折叠的
        fit: false,//自动大小
        pagination:true,//分页控件
        rownumbers:true,//行号
        fitcolumns:true,
        view: detailview,//注意1
        title: 'license列表',
        url:'ajax_license_data',
        singleSelect:true,
        method: 'post',
        columns: [
            [
                {field: 'subject', title: '名称',width:'48%',align:'center',sortable:false,
                },
                {field: 'sysUser', title: '用户',width:'48%',align:'center',sortable:false,
                    formatter: function (value, row, index) {
                        var a = value;
                        return a.username;
                    }
                },
            ]
        ],
        detailFormatter:function(index,row){//
            return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div>';
        },
        onExpandRow:function(index,row){//
            $('#ddv-'+index).datagrid({
                url:'license_showed?id='+row.id,
                fitColumns:true,
                singleSelect:true,
                height:'auto',
                columns:[[
                    {field: 'licenseTemplate', title: '版本',width:'30%',align:'center',sortable:false,
                        formatter: function (value, row, index) {
                            var a = value;
                            return a.type;
                        }
                    },
                    {field: 'notBefore', title: '开始时间',width:'25%',align:'center',sortable:false,
                        formatter:function (value, rec, index) {
                            var t = formatDate(new Date(value));
                            return  t.substring(0,10)
                        }},
                    {field: 'info', title: '说明信息',width:'30%',align:'center',sortable:false},

                    {field:'opt',title:'操作',width:'15%',align:'center', formatter:function(value,index) {
                        var name = index.username;
                        return "<shiro:hasPermission name='sys:user:add'><a class='datagrid_a_btn_worng fa fa-trash-o ' style='font-size: 10px' title='删除' onclick='deleteMenu("+index.id+");'></a></shiro:hasPermission>"+
                            "<shiro:hasPermission name='sys:user:allocation'><a class='datagrid_a_btn_seccess fa fa-user' style='margin-left: 3px;font-size: 10px' title='导出' onclick='downloads("+index.id+");'></a></shiro:hasPermission>"+
                            "<shiro:hasPermission name='sys:user:allocation'><a class='datagrid_a_btn_primary fa fa-pencil' style='margin-left: 3px;font-size: 10px' title='详情' onclick='f_info("+index.id+");'></a></shiro:hasPermission>"+
                            "<shiro:hasPermission name='sys:user:allocation'><a class='datagrid_a_btn_seccess fa fa-plus' style='margin-left: 3px;font-size: 10px' title='续期' onclick='renewaledLicense("+index.id+");'></a></shiro:hasPermission>"
                    }
                    }
                ]],
                onResize:function(){
                    $('#licenseListTab').datagrid('fixDetailRowHeight',index);
                },
                onLoadSuccess:function(){
                    setTimeout(function(){
                        $('#licenseListTab').datagrid('fixDetailRowHeight',index);
                    },0);
                    var dataGrid = $("#licenseListTab");
                    autoHeight(dataGrid,rowHeight);
                }
            });
            $('#licenseListTab').datagrid('fixDetailRowHeight',index);
        }
    });

//设置分页控件
    var p = $('#licenseListTab').datagrid('getPager');
    $(p).pagination({
        pageSize: 10,////每页显示的记录条数，默认为10
        pageList: [10,20,30,40,50],//可以设置每页记录条数的列表
        beforePageText: '第',//页数文本框前显示的汉字
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
    });
}

/**
 * 删除license的方法
 */
function batchDelete(){
    var ids = [];
    var rows = $('#licenseListTab').datagrid('getSelections');
    for(var i=0; i<rows.length; i++){
        ids.push(rows[i].id);
    }
    if(ids.length == 0){
        alertx("请至少选择一项",0);
        return ;
    }
    var idStr = ids.join(",");
    layer.confirm('确定删除所选数据吗?', {icon: 3, title:'提示'}, function(index) {
        layer.close(index);
        $.post('role_delete',{"idStr":idStr},function(data){
            if(data == 0){
                alertx('删除成功',1);
                search();
            }else{
                alertx('删除失败',2);
                search();
            }
        });
    });

}
/**
 * 更新license的方法
 */
function f_info(id){
    layer.open({
        type: 2,
        title: 'license详情展示页面',
        shadeClose: true,
        maxmin: true,
        shade: 0.8,
        area: ['50%', '70%'],
        content: 'license_show?id='+id,
        cancel:function (index) {
            layer.close(index);
            // search();
        },
        end:function (index) {
            layer.close(index);
            // search();
        }
    });

}

/**
 * license续期的方法
 */
function renewaledLicense(id){
    layer.open({
        type: 2,
        title: 'license续期页面',
        shadeClose: true,
        maxmin: true,
        shade: 0.8,
        area: ['50%', '70%'],
        content: 'license_renewal?id='+id,
        cancel:function (index) {
            layer.close(index);
            search();
        },
        end:function (index) {
            layer.close(index);
            search();
        }
    });

}

/**
 * 重置方法
 */
function reset(){
    $("#subject").val("");
    search();
    $("#subject").focus();
}

/**
 * 模糊查询方法
 */
function search(sortName,sortValue) {
    $("#licenseListTab").datagrid('load', {
        subject: $("#subject").val(),
        sortName:sortName,
        sortValue:sortValue,
    });
    $('#licenseListTab').datagrid('clearSelections');
}
function deleteMenu(id){
    top.layer.confirm('确认删除该条license?', {icon: 3, title:'提示'}, function(index) {
        top.layer.close(index);
        location.href=PATH.BASEPATH+"/admin/license/license_delete?id="+id;
    });
}
function downloads(id){
    location.href=PATH.BASEPATH+"/admin/license/download?id="+id;
}

